package com.adtech.farmProducts.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.adtech.common.widgets.load.LoadFactory;
import com.adtech.common.widgets.load.core.LoadManager;
import com.adtech.common.widgets.load.view.AbsView;
import com.adtech.farmProducts.base.BaseActivity;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.widget.ErrorView;
import com.adtech.farmProducts.widget.LoadingView;

import butterknife.BindView;

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.web_view)
    public WebView mWebView;
    private String url="";//
    /**
     * DATA
     */
    private LoadManager mLoadManager;
    public static void show(Context context,String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("URL",url);
        context.startActivity(intent);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        if(bundle!=null){
            url=bundle.getString("URL");
        }
        return super.initArgs(bundle);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        mLoadManager = LoadFactory.getInstance().register(mWebView, new AbsView.OnReloadListener() {
            @Override
            public void onReload(View view) {
                mLoadManager.showStateView(LoadingView.class);
                loadWeb();
            }
        });
        loadWeb();
    }


    /**
     * loadWeb 加载页面
     */
    private void loadWeb() {
        try {
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);// 设置支持javascript
            mWebView.setWebChromeClient(new WebChromeClient() {

                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                    try {
                        if (newProgress == 100) {
                                mLoadManager.showSuccessView();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onReceivedTitle(WebView view, String titleValue) {
                    super.onReceivedTitle(view, titleValue);
                    title.setText(titleValue);
                    if (titleValue.equals("lic")) {
                        mLoadManager.showStateView(ErrorView.class);
                    }
                }
            });

            mWebView.setWebViewClient(new WebViewClient() {

                public void onReceivedError(WebView view, int errorCode,
                                            String description, String failingUrl) {
                    super.onReceivedError(view, errorCode, description, failingUrl);
                    mLoadManager.showStateView(ErrorView.class);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if (url.contains("tel:")) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse(url));
                        startActivity(intent);
                        return true;
                    } else {
                        return super.shouldOverrideUrlLoading(view, url);
                    }
                }
            });
            mWebView.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onDestroy() {
        if (mWebView != null) {
            mWebView.destroy();
            mWebView.destroyDrawingCache();
        }
        mWebView = null;
        super.onDestroy();
    }


}
